﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Imaginecup.stateManager;
using Microsoft.Xna.Framework;
using Imaginecup.shareData;

namespace Imaginecup.gameState
{
    public abstract class GameState : DrawableGameComponent, IGameState
    {
        protected Game m_Game;
        protected GameStateManager m_GameStateMan;
        protected GraphicShareData m_GraphicData;

        private bool m_bInitState = false;       
       
        protected void ChangeState(IGameState state)
        {
            m_GameStateMan.PopState();// 스택에서 뺀다.
            m_GameStateMan.PushState(state); //추가
        }

        public virtual void AddStateEvent()
        {
            if (m_GameStateMan.GetTopState() == this)
            {
                if (!GetInitState())
                {
                    StartGameState();//상태로 들어왔을때 초괴화 하지 않았으면 init , load를 한다. 
                }
            }
        }
        
        protected bool GetInitState()
        {
            return m_bInitState;
        }

        public virtual void RemoveStateEvent()
        {
            m_bInitState = false;
            UnloadResource();
        }
        
        protected void StartGameState() // State초기화.
        {
            InitState();
            LoadStateResource();
            m_bInitState = true;
        }

        protected abstract void InitState();        //State를 초기화한다.
        protected abstract void LoadStateResource(); // 리소스 로딩
        protected abstract void UnloadResource(); // 상태변경시 리소스 해제

        public GameState()
            : base(StateShareData.GetShareData().GetGame())
        {
            m_Game = StateShareData.GetShareData().GetGame();
            m_GameStateMan = StateShareData.GetShareData().GetGameStateMan();
            m_GraphicData = GraphicShareData.GetShareData();
        }
    }
}
